package d_threadpool;

import java.util.Date;
import java.util.concurrent.*;

/*
    Callable创建线程
 */
public class CallableDemo02 {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
       ExecutorService pool = Executors.newFixedThreadPool(3);
       //创建任务类
        Callable<Date> call = new Callable<Date>() {
            @Override
            public Date call() throws Exception {
                for (int i = 0; i < 10; i++) {
                    Thread t = Thread.currentThread();
                    System.out.println(t.getName() + ":" + i);
                }
                //等待的时间
                Thread.sleep(10000);
                return new Date();
            }
        };
        //将任务类交给线程池
        Future<Date> f = pool.submit(call);
        //超时等待
        try {
            Date date = f.get(2, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            System.out.println("超时了!!!");
        }

        System.out.println("主方法的线程执行了!!!");

        pool.shutdown();
    }
}
